<?php

class newsDetails {

    function get_newsDetails($id, $page) {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $result = $connect->query("SELECT id, author, title, thumb, summary, body, SUBSTRING(body, 1, 600)AS shortBody, time, category, tags FROM posts order by time DESC LIMIT 10");
        while($row = $result->fetch_object()) {
            $numrows = $result->num_rows;

            if($numrows > 0) {
                $this->checkId = true;
            }
            else {
                $this->checkId = false;
            }

            if($row->draft == "0") {
                $this->draft = true;
            }
            else {
                $this->draft = false;
            }

            if($row->author == $_SESSION['username']) {
                $this->author = "You";
            }
            else {
                $this->author = $row->author;
            }

            if(!empty($row->gallery)) {
                $this->gallery = $row->gallery;
                $urls = explode(', ', $this->gallery);
                foreach($urls as $key => $value) {
                    $images[$key] = '<li><img src="<?php print(DATA_PATH); ?>/images/posts/'.$value.'" title="'.$value.'" alt="'.$value.'" /></li>';
                }
                $this->gallery = implode('', $images);
            }

            if(!empty($row->links)) {
                $this->linkString = $row->links;
                $urls = explode(', ', $this->linkString);
                foreach($urls as $key => $value) {
                    $links[$key] = '<li><a href="http://'.$value.'" rel="external" title="'.$value.'">'.$value.'</a></li>';
                }
                $this->linkString = implode(' | ', $links);
            }
            else {
                $this->linkString = "";
            }

            if(!empty($row->tags)) {
                $this->tags = $row->tags;
                $urls = explode(', ', $this->tags);
                foreach($urls as $key => $value) {
                    $tag[$key] = '<li><a href="page/archive/tag/'.strtolower($value).'">'.ucfirst($value).'</a></li>';
                }
                $this->tags = implode(' | ', $tag);
            }
            else {
                $this->tags = "";
            }

            if($row->shortBody < $row->body){
                $shortBody = $row->shortBody.'... <a href="page/2/news/article/'.$row->id.'/'.htmlentities(strtolower(str_replace( " ", "-", $row->title))).'" title="'.$row->title.'">Complete article</a>';
            }
            else{
                $shortBody = $row->shortBody;
            }

            $userQuery = $connect->query("SELECT id FROM users WHERE username = '".$row->author."'");
            $userRow = $userQuery->fetch_object();

            $this->postDetails[] = array(
                "id" => $row->id,
                "idCheck" => $this->checkId,
                "draftCheck" => $this->draft,
                "author" => $this->author,
                "realAuthor" => $row->author,
                "authorId" => $userRow->id,
                "title" => $row->title,
                "thumb" => $row->thumb,
                "summary" => $row->summary,
                "body" => $row->body,
                "shortBody" => $shortBody,
                "category" => $row->category,
                "time" => date('l F jS Y m:s',strtotime($row->time)),
                "gallery" => $this->gallery,
                "links" => $this->linkString,
                "tags" => $this->tags
            );
        }
        return $this->postDetails;
        $result->close();
        $userQuery->close();
    }
}

?>